package controllers;

import java.awt.event.ActionListener;
import java.util.Vector;
import dialogs.AbstractDialog;
import system.ExceptionEvent;
import system.ExceptionListener;
import system.View;

public abstract class 
AbstractController 
implements ExceptionListener, ActionListener
{	public abstract int validateInsert(Vector<String> s);
	public abstract void show();
	public abstract int validateDelete(Vector<String> s);
	public int validateEntries(Vector<String> s) { return 0; }
	public void rollBack() { }
	
	protected View view = null;

	// constants used for describing the outcome of an operation
	public static final int OPERATIONSUCCESS = 0;
	public static final int OPERATIONFAILED = 1;
	public static final int VALIDATIONERROR = 2;
	
	public 
	AbstractController(View v){	view = v; }

	/**************************************************************************
	 * Private and Protected Helper Methods
	 *************************************************************************/
	
	protected int 
	operationSuccess()
	{	view.updateStatusBar("Operation successful.");
    	show();
    	return OPERATIONSUCCESS;
	}
	
	protected int
	operationFailure()
	{	view.updateStatusBar("Operation failed.");
    	return OPERATIONFAILED; 
	}
	
	// called by subclasses when they wish to properly display a dialog
	protected void 
	showDialogWindow(AbstractDialog d)
	{	d.pack();
	    view.centerWindow(d);
	    d.setVisible(true);
	}
	
	/********************************************************************
	 * ExceptionListener Interface Methods
	 *******************************************************************/
	
	// 	called by AbstractModels (and therefore all models) and both the 
	//	CustomTable and CustomTableModel
	@Override
	public void 
	exceptionGenerated(ExceptionEvent ex) 
	{	String message = ex.getMessage();
	
		if (message != null) view.updateStatusBar(ex.getMessage());
		else view.updateStatusBar("An exception occurred!");
	}
	
	
}
